﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;


#region using namespace
using System.Collections.Generic;
using System.Linq;
using DTO;
#endregion

namespace DAO
{
    public class QuanHuyenDAO
    {
        // tim quan huyen dua vao ma quan huyen
        public QuanHuyenDTO Search(int id)
        {
            QuanHuyenDTO quanhuyenDTO = null;
            List<QuanHuyenDTO> lstquanhuyen = getAllQuanHuyen();

            foreach (QuanHuyenDTO duong in lstquanhuyen)
            {
                if (duong.MaQuanHuyen == id)
                {
                    quanhuyenDTO = new QuanHuyenDTO();

                    quanhuyenDTO.MaQuanHuyen = duong.MaQuanHuyen;
                    quanhuyenDTO.TenQuanHuyen = duong.TenQuanHuyen;
                    break;
                }
            }

            return quanhuyenDTO;
        }

        //lay tat ca cac thuoc tinh cua bang quan huyen.
        public static List<QuanHuyenDTO> getAllQuanHuyen()
        {
            IList<QUANHUYEN> ilstquanhuyen;
            List<QuanHuyenDTO> lstquanhuyen = new List<QuanHuyenDTO>();
            QuanHuyenDTO quanhuyen = null;

            using (var context = new DataContext(GlobalDatabase.ConnectString))
            {
                ilstquanhuyen = (from qh in context.QUANHUYENs select qh).ToList();
            }

            for (int i = 0; i < ilstquanhuyen.Count; i++)
            {
                quanhuyen = new QuanHuyenDTO();

                quanhuyen.MaQuanHuyen = ilstquanhuyen[i].MaQuanHuyen;
                quanhuyen.TenQuanHuyen = ilstquanhuyen[i].TenQuanHuyen;

                lstquanhuyen.Add(quanhuyen);
            }

            return lstquanhuyen;
        }
    }
}
